package cn.stylefeng.guns.modular.business.timer.delay;

import cn.stylefeng.guns.modular.business.frequency.banner.config.Application;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/11/26
 **/
@Getter
@Setter
public class DelayTask<T> implements Delayed {

    /**默认延迟3天*/
    private final static long DELAY = Application.ORDER_AUTO_ARRIVE_TIME;//7 * 24 * 60 * 60 * 1000L;
    /**数据id*/
    private String dataId;
    /**开始时间*/
    private long startTime;
    /**到期时间*/
    private long expire;
    /**创建时间*/
    private Date now;
    private T data;

    public DelayTask(String dataId, long startTime, long secondsDelay) {
        super();
        this.dataId = dataId;
        this.startTime = startTime;
        this.expire = startTime + (secondsDelay * 1000);
        this.now = new Date();
    }

    public DelayTask(String dataId, long startTime) {
        super();
        this.dataId = dataId;
        this.startTime = startTime;
        this.expire = startTime + DELAY;
        this.now = new Date();
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

}
